﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;

namespace GenTheme.Parsing
{
    // Just for testing
    internal class Parser
    {
        private Color[,] arrayToParse = null;
        private List<List<Color?>> lLTobjs = new List<List<Color?>>();

        public Parser(string fileName)
        { 
            // Getting bitmap
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(System.Drawing.Image.FromFile(fileName));
            bmp.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);

            // Forms pixel array
            this.arrayToParse = new Color[bmp.Height, bmp.Width];
            for (int i = 0; i < bmp.Height; i++)
            {
                for (int j = 0; j < bmp.Width; j++)
                {
                    this.arrayToParse[i, j] = (Color)ColorConverter.ConvertFromString("#" + bmp.GetPixel(j, i).Name);
                }
            }

            // Generate ThemeObjects
            int rpX = 0;
            int rpY = 0;
            do
            {
                // Get pixel to start from
                StepInfo si = new StepInfo(this.arrayToParse, rpX, rpY);

                if (si.Value != -1)
                {
                    //Check if there is additional square on the left side
                    List<Color?> ltj = new List<Color?>();
                    if (si.X - si.Value >= 0)
                    {
                        ltj.Add(this.arrayToParse[si.Y, si.X - si.Value / 2]);
                    }

                    //Add sqaures from the right side of the pixel
                    int i = si.Value + si.Value / 2;
                    while (i < this.arrayToParse.GetLength(1))
                    {
                        ltj.Add(this.arrayToParse[si.Y, i]);
                        i += si.Value;
                    }

                    this.lLTobjs.Add(ltj);
                    rpY = si.Y;
                }
                rpX = 0;
                rpY++;
            }
            while (rpY < this.arrayToParse.GetLength(0));
        }

        /// <summary>
        /// Parsed Result
        /// </summary>
        public List<Color?>[] Result
        {
            get
            {
                return this.lLTobjs.ToArray();
            }
        }
    }
}
